<?php
define('USER_SEX_MALE',		'M');
define('USER_SEX_FEMALE',	'F');
define('USER_TYPE_ADMIN',	'ADMIN');
define('USER_TYPE_USER',	'USER');
class UserPasswordException extends WException {}
class Cl_User extends WdkClass 
{
	protected
		/******************************************************/
		//	원본 테이블 정보
		/******************************************************/
		$user_seq			= 0					,	//	사용자 키값
		$user_type			= USER_TYPE_USER	,	//	사용자 로그인 아이디
		$user_login_id		= ''				,	//	사용자 로그인 아이디
		$user_login_passwd	= ''				,	//	로그인 비밀번호
		$fk_user_group_seq	= 0					,	//	그룹 키값
		$user_identity_no	= ''				,	//	주민번호
		$user_name			= ''				,	//	사용자명
		$user_nick			= ''				,	//	사용자 닉네임
		$user_birth_date	= ''				,	//	생일
		$user_phone			= ''				,	//	연락처
		$user_mobile		= ''				,	//	핸드폰번호
		$user_email			= ''				,	//	이메일주소
		$user_sex			= USER_SEX_MALE		,	//	성별
		$user_memo			= ''				,	//	메모사항
		$user_validate_time	= 0					,	//	인증시간
		$user_insert_time	= 0					,	//	가입시간
		$user_del_flag		= 'F'				,	//	삭제여부
		/******************************************************/
		//	추가 정보
		/******************************************************/
		$user_login_passwd2	= ''				,	//	로그인 비밀번호
		$user_group_name	= ''				,	//
		$user_login_time	= ''				;	//
	
	/**
	 * 	
	 */
	function __construct()
	{
		parent::__construct();
		self::getDbo();
	}
	
	/**
	 *	
	 */
	public function __init($data)
	{
		parent::__init($data);
		self::dformat();
	}
	
	/**
	 *	
	 */
	public function load($user_seq)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		self::__init($dbo->cLoad($user_seq));
	}
	
	public function loadByLoginId($user_login_id)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		self::__init($dbo->cLoadByLoginId($user_login_id));
	}
	
	/**
	 *	
	 */
	public function dformat()
	{
		$pwd = Wdk::helper('Passwd');
		if($pwd->isDecryptable($this->user_login_passwd))
		{
			$this->user_login_passwd2	= $this->user_login_passwd;
			$this->user_login_passwd	= $pwd->decrypt($this->user_login_passwd);
		}
		if(strlen($this->user_identity_no) == 48)
		{
			$this->user_identity_no		= substr($this->user_identity_no, 0, 8).'******';
		}
		
		list(	$this->user_mobile1,
				$this->user_mobile2,
				$this->user_mobile3) = explode('-', $this->user_mobile);
		list(	$this->user_phone1,
				$this->user_phone2,
				$this->user_phone3) = explode('-', $this->user_phone);
		list(	$this->user_birth_date1,
				$this->user_birth_date2,
				$this->user_birth_date3) = explode('-', $this->user_birth_date);
	}
	
	/**
	 * 
	 */
	public function autoUpdate($log='')
	{
		if((int)$this->user_seq > 0)
		{
			$this->update($log);
		}
		else
		{
			$this->insert($log);
		}
	}
	
	/**
	 *	
	 */
	public function insert($log='Insert')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		$pwd = Wdk::helper('Passwd');
		/*------------------------------------------------------------*/
		$this->user_validate_time	= 0;
		$this->user_insert_time		= time();
		if($pwd->isEncryptable($this->user_login_passwd) == true)
		{
			$this->user_login_passwd	= $pwd->encrypt($this->user_login_passwd);
		}
		$this->user_del_flag		= 'F';
		$this->encrypt_identity_no();
		
		$data = array(	'user_login_id'			=>	$this->user_login_id,
						'user_login_passwd'		=>	$this->user_login_passwd,
						'fk_user_group_seq'		=>	$this->fk_user_group_seq,
						'user_identity_no'		=>	$this->user_identity_no,
						'user_name'				=>	$this->user_name,
						'user_nick'				=>	$this->user_nick,
						'user_birth_date'		=>	$this->user_birth_date,
						'user_email'			=>	$this->user_email,
						'user_mobile'			=>	$this->user_mobile,
						'user_phone'			=>	$this->user_phone,
						'user_sex'				=>	$this->user_sex,
						'user_memo'				=>	$this->user_memo,
						'user_validate_time'	=>	$this->user_validate_time,
						'user_insert_time'		=>	$this->user_insert_time,
						'user_del_flag'			=>	$this->user_del_flag
					);
		$this->user_seq = $dbo->insert($data);
		$this->backup($log);
	}
	
	/**
	 * 
	 */
	public function update($log='update')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		$pwd = Wdk::helper('Passwd');
		/*------------------------------------------------------------*/
		
		$data = array(	'fk_user_group_seq'		=>	$this->fk_user_group_seq,
						'user_name'				=>	$this->user_name,
						'user_nick'				=>	$this->user_nick,
						'user_birth_date'		=>	$this->user_birth_date,
						'user_email'			=>	$this->user_email,
						'user_mobile'			=>	$this->user_mobile,
						'user_phone'			=>	$this->user_phone,
						'user_sex'				=>	$this->user_sex,
						'user_memo'				=>	$this->user_memo
					);
		if($pwd->isEncryptable($this->user_login_passwd))
		{
			$this->user_login_passwd	= $pwd->encrypt($this->user_login_passwd);
			$data['user_login_passwd']	= $this->user_login_passwd;
		}
		if(strlen($this->user_identity_no) == 14 && substr($this->user_identity_no, -6) != '******')
		{
			$this->encrypt_identity_no();
			$data['user_identity_no']	= $this->user_identity_no;
		}
		$dbo->update($data, array("user_seq = ".$this->user_seq));
		$this->backup($log);
	}
	
	/**
	 * 
	 */
	public function approve($log='approve')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		
		$this->user_validate_time = time();
		
		$data = array(	'user_validate_time'	=>	$this->user_validate_time
					);
		$dbo->update($data, array("user_seq = ".$this->user_seq));
		$this->backup($log);
	}
	
	/**
	 * 
	 */
	public function delete($log='delete')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->delete(array("user_seq = ".$this->user_seq));
		$this->backup($log);
	}
	
	/**
	 *	
	 */
	public function login($login_id='', $login_passwd='')
	{
		$this->loadByLoginId($login_id);
		/*------------------------------------------------------------*/
		$pwd = Wdk::helper('Passwd');
		/*------------------------------------------------------------*/
		if( !$pwd->isEqual($this->user_login_passwd2, $login_passwd))
		{
			throw new UserPasswordException("");
		}
	}
	
	/**
	 *	
	 */
	private function backup($log='')
	{
// 		/*------------------------------------------------------------*/
// 		$dbo = self::getDbo();
// 		/*------------------------------------------------------------*/
// 		$dbo->log($this->user_seq, $log, $this->backup_data);
	}
	
	/**
	 *	
	 */
	private function encrypt_identity_no()
	{
		if(strlen($this->user_identity_no) == 14)
		{
			$this->user_identity_no	= substr($this->user_identity_no, 0, 8).sha1($this->user_identity_no);
		}
	}
	
	/**
	 * 
	 */
	public function getDbo()
	{
		return Wdk::model(substr(__CLASS__, 3));
	}
}